package com.crlui.manager
{
	import com.crlui.core.IFactory;
	import com.crlui.core.IPoolable;
	
	
	public class Pool
	{
		private var list:Vector.<IPoolable>;
		private var max:int;
		
		private var factory:IFactory;
		public function Pool(factory:IFactory)
		{
			list=new Vector.<IPoolable>();
			
			this.factory=factory;
		}
		
		public function set maxSize(value:int):void{
			this.max=value;
			
		}
		
		public function add(value:IPoolable):Boolean{
			if(list.length==max){
				value.dispose();
				return false;
			}
			value.sleep();
			list.push(value);
			
			return true;
		}
		
		public function get():IPoolable{
			var item:IPoolable;
			if(list.length){
				item=list.pop();
				item.awaken();
				return item;
			}
			return factory.newInstance();
		}
	}
}